package cn.momoshenchi.medicine.controller;

import cn.momoshenchi.medicine.enumerate.Add;
import cn.momoshenchi.medicine.enumerate.Update;
import cn.momoshenchi.medicine.pojo.BaseDicDepartment;
import cn.momoshenchi.medicine.pojo.BaseDicDrugFrequency;
import cn.momoshenchi.medicine.pojo.BaseDicDrugUsage;
import cn.momoshenchi.medicine.pojo.BaseOrganization;
import cn.momoshenchi.medicine.service.*;
import cn.momoshenchi.medicine.util.AssertUtil;
import cn.momoshenchi.medicine.util.ResultInfo;
import cn.momoshenchi.medicine.vo.HospitalByDept;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/7/15 - 9:09
 */
@RestController
public class EnumController
{
    @Autowired
    private BaseDicDrugFrequencyService baseDicDrugFrequencyService;

    @Autowired
    private BaseDicDrugUsageService baseDicDrugUsageService;

    @Autowired
    private BaseDoctorService doctorService;

    @Autowired
    private BaseOrganizationService organizationService;

    @Autowired
    private BaseDicDepartmentService departmentService;

    @GetMapping("/usage/list")
    public ResultInfo usage()
    {
        return  new ResultInfo(baseDicDrugUsageService.list());
    }


    @GetMapping("/frequency/list")
    public ResultInfo frequency()
    {
        return  new ResultInfo(baseDicDrugFrequencyService.list());
    }

    @GetMapping("/dept/list")
    public ResultInfo dept()
    {
        return  new ResultInfo(departmentService.list());
    }
    /**
     * 根据根据医院查找医生,然后根据部门进行分类
     * @param orgId
     * @return
     */
    @GetMapping("/org/dept/{orgId}")
    public ResultInfo getByOrgIdOrderByDept(@PathVariable String orgId)
    {
        List< HospitalByDept> hospitalByDept=doctorService.getByOrgIdOrderByDept(orgId);
        return  new ResultInfo(hospitalByDept);
    }

    @PostMapping("/usage/add")
    public ResultInfo usageAdd(@RequestBody @Validated(Add.class)  BaseDicDrugUsage usage)
    {
        AssertUtil.isTrue(!baseDicDrugUsageService.save(usage),"用法添加失败");
        return  new ResultInfo();
    }
    @GetMapping("/frequency/get/{id}")
    public ResultInfo frequencyGet(@PathVariable String id)
    {
        BaseDicDrugFrequency drugFrequency = baseDicDrugFrequencyService.getById(id);
        return  new ResultInfo(drugFrequency);
    }

    @GetMapping("/usage/get/{id}")
    public ResultInfo usageGet(@PathVariable String id)
    {
        BaseDicDrugUsage drugUsage = baseDicDrugUsageService.getById(id);
        return  new ResultInfo(drugUsage);
    }

    @PostMapping("/frequency/add")
    public ResultInfo frequencyAdd(@RequestBody  @Validated(Add.class) BaseDicDrugFrequency frequency)
    {
        AssertUtil.isTrue(!baseDicDrugFrequencyService.save(frequency),"用药频率添加失败");
        return  new ResultInfo();
    }

    @PostMapping("/frequency/delete/{id}")
    public ResultInfo frequencyDelete(@PathVariable String id)
    {
        AssertUtil.isTrue(!baseDicDrugFrequencyService.removeById(id),"用药频率删除失败");
        return  new ResultInfo();
    }

    @PostMapping("/usage/delete/{id}")
    public ResultInfo usageDelete(@PathVariable String id)
    {
        AssertUtil.isTrue(!baseDicDrugUsageService.removeById(id),"用法删除失败");
        return  new ResultInfo();
    }


    @PostMapping("/usage/update")
    public ResultInfo usageUpdate(@RequestBody    @Validated(Update.class) BaseDicDrugUsage usage)
    {
        AssertUtil.isTrue(!baseDicDrugUsageService.updateById(usage),"用法更新失败");
        return  new ResultInfo();
    }


    @PostMapping("/frequency/update")
    public ResultInfo frequencyUpdate(@RequestBody  @Validated(Update.class) BaseDicDrugFrequency frequency)
    {
        AssertUtil.isTrue(!baseDicDrugFrequencyService.updateById(frequency),"用药频率更新失败");
        return  new ResultInfo();
    }

    @PostMapping("/dept/add")
    public ResultInfo deptAdd(@RequestBody  @Validated(Add.class) BaseDicDepartment dicDepartment)
    {
        AssertUtil.isTrue(!departmentService.save(dicDepartment),"部门添加失败");
        return  new ResultInfo();
    }

    @PostMapping("/dept/update")
    public ResultInfo deptUpdate(@RequestBody  @Validated(Update.class) BaseDicDepartment dicDepartment)
    {
        AssertUtil.isTrue(!departmentService.updateById(dicDepartment),"部门更新失败");
        return  new ResultInfo();
    }

    @PostMapping("/dept/delete/{id}")
    public ResultInfo deptDelete(@PathVariable String id)
    {
        AssertUtil.isTrue(!departmentService.removeById(id),"部门删除失败");
        return  new ResultInfo();
    }

    @GetMapping("/dept/get/{id}")
    public ResultInfo deptGet(@PathVariable String id)
    {
        BaseDicDepartment dicDepartment = departmentService.getById(id);
        return  new ResultInfo(dicDepartment);
    }
}
